package com.fengdi.service.impl;

import com.fengdi.mapper.UserMapper;
import com.fengdi.pojo.Result;
import com.fengdi.pojo.User;
import com.fengdi.service.serviceInterface.UserService;
import com.fengdi.utils.JwtUtil;
import com.fengdi.utils.Md5Util;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.validation.annotation.Validated;

import java.time.LocalDateTime;
import java.util.HashMap;
import java.util.Map;

@Service
public class UserServiceImpl implements UserService {
    @Autowired
    private UserMapper userMapper;

    @Override
    public User findByUsername(String username) {
        if(username != null && !username.isEmpty()) {
            return this.userMapper.findByUsername(username);
        }
        return null;
    }

    @Override
    public void userRegister(String username, String password) {
        // 密码加密
        String password_MD5_str = Md5Util.getMD5String(password);
        userMapper.insertUser(username, password_MD5_str);
    }

    @Override
    public Result<?> userLogin(String username, String password) {
        User loginUser = this.findByUsername(username);
        if(loginUser != null) {
            if(Md5Util.checkPassword(password, loginUser.getPassword())) { // 密码验证成功
                // 生成Token
                Map<String, Object> payload = new HashMap<>();
                payload.put("id", loginUser.getId());
                payload.put("username", loginUser.getUsername());
                String s = JwtUtil.genToken(payload);
                return Result.success(s);
            }else {
                return Result.error("用户名或密码不正确，请检查后重新输入");
            }
        }else {
            return Result.error("用户不存在");
        }
    }

    /**
     * @param username : 用户名
     * @return User结果
     * @author FengDi
     * @Description 获取用户详细信息
     */
    @Override
    public Result<User> getUserInfo(String username) {
        return Result.success(this.findByUsername(username));
    }

    /**
     * @param user : 前端传来的用户数据对象
     * @return 更新结果
     * @author FengDi
     * @Description 更新用户数据
     */
    @Override
    public Result<?> update(User user) {
        user.setUpdateTime(LocalDateTime.now());
        try {
            this.userMapper.updateUser(user);
            return Result.success();
        }catch (Exception e) {
            return Result.error(e.getMessage());
        }
    }

    /**
     * @param avatarUrl : 图片路径
     * @return 更新结果
     * @author FengDi
     * @Description 更新用户头像
     */
    @Override
    public Result<?> updateUserAvatar(String username, String avatarUrl) {
        try {
            this.userMapper.updateUserPic(username, avatarUrl);
            return Result.success();
        }catch (Exception e) {
            return Result.error(e.getMessage());
        }
    }

    /**
     * @param username : 用户名
     * @param pwdMap   : 新旧密码集合
     * @return 修改结果
     * @author FengDi
     * @Description 修改用户密码
     */
    @Override
    public Result<?> updateUserPwd(String username, Map<String, String> pwdMap) {
        try{
            // 从数据库获取旧密码进行比对
            User user = this.findByUsername(username);
            // 若比对成功，则核对新密码是否符合条件
            if(Md5Util.checkPassword(pwdMap.get("old_pwd"), user.getPassword()) && pwdMap.get("new_pwd").equals(pwdMap.get("re_pwd"))) {
                // 新密码符合条件存入新密码替换旧密码
                this.userMapper.updateUserPwdByUsername(username, Md5Util.getMD5String(pwdMap.get("new_pwd")), LocalDateTime.now().toString());
                return Result.success();
            }else {
                // 任意条件不满足直接返回失败
                return Result.error("旧密码不正确或两次输入的新密码不相同");
            }
        }catch (Exception e) {
            return Result.error(e.getMessage());
        }

    }
}
